package com.isoftstone.securityframework.device.local;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.isoftstone.securityframework.api.Permission;
import com.isoftstone.securityframework.auth.realm.SubjectPermissionRealm;
import com.isoftstone.securityframework.device.Device;
import com.isoftstone.securityframework.device.DeviceGroupManager;
import com.isoftstone.securityframework.device.domain.DeviceGroup;

@Service
public class DeviceGroupPermissionRealm implements SubjectPermissionRealm {

	@Resource
	private DeviceGroupManager groupManager = null;
	@Override
	public List<Permission> getSubjectPermission(Subject subject) {
		Device d = (Device) subject;
		List<DeviceGroup> groups = d.getGroups();
		List<Permission> allPermission = new ArrayList<Permission>();
		for (DeviceGroup g : groups) {
			List<Permission> ps = groupManager.permissions(g);
			allPermission.addAll(ps);
		}
		return allPermission;
	}

}
